<?php
namespace Home\Controller;
use Think\Controller;

class ProductController extends \Common\Controller\UserController {
    //商品列表
    public function product() {
    	$where = ' 1 = 1 ';
		$pageurl = '/user/product?';

		$productname = I('get.productname');
    	if(!empty($productname)) {
    		$this -> assign('productname', $productname);
    		$where .= ' AND productname LIKE "%' . $productname . '%" ';
    		$pageurl .= 'productname=' . $productname . '&';
    	}

    	$page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
    	$this -> assign('page', $page);

    	$pagesize = 8;
    	$this -> assign('pagesize',$pagesize);
    	$this -> assign('pageurl', $pageurl);

		$products = M('product');

        $data = $products -> where($where) -> order(" id desc") -> limit(($page-1)*$pagesize, $pagesize) -> select();
        $this -> assign('room',$data);

        $count = $products -> where($where) -> count();
        $this -> assign('total', $count);
        
        if($count%$pagesize==0) {
            $this -> assign('count', $count/$pagesize);
		} else {
			$this -> assign('count', (int)floor($count/$pagesize)+1);
		}

        $integral = M('integral') -> where('userid=' . session('userid')) -> find();
        $this -> assign('integral', $integral);
        $this -> display();
    }

    //商品详情页
    public function product_view() {
        $id = I('get.id');
        if(empty($id)) {
            echo"<script>alert('没有相应的商品！');history.go(-1);</script>";
        }

        $data = M('product') -> where('id = ' . $id) -> find();
        $this -> assign('room', $data);
        $this -> assign('pics', json_decode($data['pics']));
        $this -> display();
    }

    //商品详情页
    public function product_order() {
        $id = I('get.id');
        if(empty($id)) {
            echo"<script>alert('没有相应的商品！');history.go(-1);</script>";
        }

        $this -> assign('room', M('product') -> where('id = ' . $id) -> find());

        $userid = session('userid');
        $this->assign('user', M('user') -> where('id=' . $userid)->find());

		//$add = M('user_address') -> where('userid='.$userid) -> order('def desc') -> select();
		//$this -> assign('add',$add);

        $this -> assign('int', M('integral') -> where('userid='.$userid) -> find());

        $this->display();
    }
}
?>